package com.leather.news.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.news.service.NewsService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private RestTemplate restTemplate;

    private static String newsDetail = "http://jw.cqjtu.edu.cn";

    private static String jwtzUrl = "http://jw.cqjtu.edu.cn/jxxx/tzgg1.htm";

    private static Integer pages = 0;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result getNewsList(int categoryId) {
        int val = 0;
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        if (categoryId != 0) {
            if(pages==0) {
                pages = 32;
            }
            jwtzUrl = "http://jw.cqjtu.edu.cn/jxxx/tzgg1/" + (pages - categoryId) + ".htm";
            val = 5;
        }else{
            if(redisTemplate.hasKey("news")) {
                String news = operations.get("news");
                return Result.success(JSON.parse(news));
            }
            jwtzUrl = "http://jw.cqjtu.edu.cn/jxxx/tzgg1.htm";
            val = 0;
        }
        String entity = "";
        try {
            entity = restTemplate.getForObject(jwtzUrl, String.class);
        } catch (Exception e) {
            return Result.error(CodeMsg.NET_OFFLINE);
        }
        Document document = Jsoup.parse(entity);
        Element element = document.getElementById("fanye51038");
        String total = element.html();
        total = total.substring(1, 4);
        if (pages == 0) {
            pages = Integer.parseInt(total.trim());
            pages = pages / 10 + (pages % 10 == 0 ? 0 : 1);
        }
        Elements dt = document.getElementsByTag("dt");
        JSONArray array = new JSONArray();
        for (int i=val;i<val+10;i++) {
            Element e = dt.get(i);
            JSONObject obj = new JSONObject();
            Elements a = e.getElementsByTag("a");
            String href = a.attr("href");
            obj.put("url", href);
            Elements contitle = e.getElementsByClass("contitle");
            Elements contime = e.getElementsByClass("contime");
            obj.put("title", contitle.html());
            obj.put("time", contime.html());
            array.add(obj);
        }
        if(categoryId==0) {
            String news = array.toJSONString();
            operations.set("news",news,3600*6,TimeUnit.SECONDS);
        }
        return Result.success(array);
    }

    @Override
    public Result getNewsByNewsId(String newsId) {
        String url = newsDetail + newsId;
        String response = restTemplate.getForObject(url, String.class);
        Document document = Jsoup.parse(response);
        JSONObject object = new JSONObject();
        Elements con_title = document.getElementsByClass("con_title").get(0).getElementsByTag("h3");
        Element element = con_title.get(0);
        object.put("title", element.html());
        Element body = document.getElementsByClass("newscontent").get(0);
        object.put("content", body.html());
        return Result.success(object);
    }

}
